package com.gxuwz.medical.domain.family;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.exception.DbException;
import com.mysql.fabric.xmlrpc.base.Data;

public class Family {

	private String familycode;
	
	private String areacode;

	private String familyattribute;

	private String population;

	private String farmpopulation;

	private String familyaddress;

	private Timestamp creattime;

	private String registrar;

	private String familyholdername;	

	public Family() {

	}

	public String getFamilycode() {
		return familycode;
	}

	public void setFamilycode(String familycode) {
		this.familycode = familycode;
	}

	public String getAreacode() {
		return areacode;
	}

	public void setAreacode(String areacode) {
		this.areacode = areacode;
	}

	public String getFamilyattribute() {
		return familyattribute;
	}

	public void setFamilyattribute(String familyattribute) {
		this.familyattribute = familyattribute;
	}

	public String getPopulation() {
		return population;
	}

	public void setPopulation(String population) {
		this.population = population;
	}

	public String getFarmpopulation() {
		return farmpopulation;
	}

	public void setFarmpopulation(String farmpopulation) {
		this.farmpopulation = farmpopulation;
	}

	public String getFamilyaddress() {
		return familyaddress;
	}

	public void setFamilyaddress(String familyaddress) {
		this.familyaddress = familyaddress;
	}

	public Timestamp getCreattime() {
		return creattime;
	}

	public void setCreattime(Timestamp creattime) {
		this.creattime = creattime;
	}

	public String getRegistrar() {
		return registrar;
	}

	public void setRegistrar(String registrar) {
		this.registrar = registrar;
	}

	public String getFamilyholdername() {
		return familyholdername;
	}

	public void setFamilyholdername(String familyholdername) {
		this.familyholdername = familyholdername;
	}

	

	@Override
	public String toString() {
		return "Family [familycode=" + familycode + ", areacode=" + areacode
				+ ", familyattribute=" + familyattribute + ", population="
				+ population + ", farmpopulation=" + farmpopulation
				+ ", familyaddress=" + familyaddress + ", creattime="
				+ creattime + ", registrar=" + registrar
				+ ", familyholdername=" + familyholdername + "]";
	}

	public Family(String familycode, String areacode, String familyattribute,
			String population, String farmpopulation, String familyaddress,
			Timestamp creattime, String registrar, String familyholdername) {
		super();
		this.familycode = familycode;
		this.areacode = areacode;
		this.familyattribute = familyattribute;
		this.population = population;
		this.farmpopulation = farmpopulation;
		this.familyaddress = familyaddress;
		try {
			this.creattime = stringToTimestamp(creattime.toString());
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		this.registrar = registrar;
		this.familyholdername = familyholdername;
	}

	public static Timestamp stringToTimestamp(String dateStr)
			throws java.text.ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		try {
			Date date = (Date) sdf.parse(dateStr);
			date.getTime();
			cal.setTime(date);
			return new Timestamp(cal.getTimeInMillis());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.setTime(new Date());
		return new Timestamp(cal.getTimeInMillis());
	}

	private void loadDB(String familycode) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_family where familycode=?");
			pstmt.setString(1, this.familycode);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.familycode = rs.getString("familycode");
				this.areacode = rs.getString("areacode");
				this.familyattribute = rs.getString("familyattribute");
				this.population = rs.getString("population");
				this.farmpopulation = rs.getString("farmpopulation");
				this.familyaddress = rs.getString("familyaddress");
				this.creattime = rs.getTimestamp("creattime");
				this.registrar = rs.getString("registrar");
				this.familyholdername = rs.getString("familyholdername");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

	public Family(String familycode) throws DbException {
		try {
			this.familycode = familycode;
			loadDB(familycode);
		} catch (SQLException e) {
			throw new DbException("加载id=" + familycode + "行政区域记录失败", e);
		}
	}

	public void addFamily(String areacode, String familyattribute,
			String population, String farmpopulation, String familyaddress,
			Object creattime, String registrar, String familycode,
			String familyholdername) throws Exception {
		Connection conn = null;
		try {
			// 初始化属性
			this.areacode = areacode;
			this.familyattribute = familyattribute;
			this.population = population;
			this.farmpopulation = farmpopulation;
			this.familyaddress = familyaddress;
			Object time=new Data();
			try {
				this.creattime = stringToTimestamp(time.toString());
			} catch (java.text.ParseException e) {
				e.printStackTrace();
			}
			this.registrar = registrar;
			this.familycode = familycode;
			this.familyholdername = familyholdername;
			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_family(areacode,familyattribute,population,farmpopulation,"
					+ "familyaddress,creattime,registrar,familycode,familyholdername)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			int i = 1;
			pstmt.setString(i++, this.areacode);
			pstmt.setString(i++, this.familyattribute);
			pstmt.setString(i++, this.population);
			pstmt.setString(i++, this.farmpopulation);
			pstmt.setString(i++, this.familyaddress);
			pstmt.setTimestamp(i++, this.creattime);
			pstmt.setString(i++, this.registrar);
			pstmt.setString(i++, this.familycode);
			pstmt.setString(i++, this.familyholdername);
			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}

	public void edit(String areacode, String familyattribute,
			String population, String farmpopulation, String familyaddress,
			Object creattime, String registrar, String familycode,
			String familyholdername) throws SQLException {
		Connection conn = null;
		try {
			this.familycode = familycode;
			this.areacode = areacode;
			this.familyattribute = familyattribute;
			this.population = population;
			this.farmpopulation = farmpopulation;
			this.familyaddress = familyaddress;
			try {
				this.creattime = stringToTimestamp(creattime.toString());
			} catch (java.text.ParseException e) {
				e.printStackTrace();
			}
			this.registrar = registrar;
			this.familyholdername = familyholdername;
			conn =DbUtil.getConn();
			conn.setAutoCommit(false);
			this.updateToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void updateToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			// 定义构造update SQL语句字符串变量sql
			String sql = "update t_family set familyattribute=?,population=?,farmpopulation=?,"
					+ "familyaddress=?,creattime=?,registrar=?,areacode=?,familyholdername=? where familycode=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int i = 1;
			pstmt.setString(i++, this.familyattribute);
			pstmt.setString(i++, this.population);
			pstmt.setString(i++, this.farmpopulation);
			pstmt.setString(i++, this.familyaddress);
			pstmt.setTimestamp(i++, this.creattime);
			pstmt.setString(i++, this.registrar);
			pstmt.setString(i++, this.areacode);
			pstmt.setString(i++, this.familyholdername);
			pstmt.setString(i++, this.familycode);
			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}

	}
	
	public void del(String familycode)throws SQLException{

		Connection conn=null;
       try{
    	   //1:对象属性赋值
    	    this.familycode =familycode;
    	    conn=DbUtil.getConn();
    	   //2；调用delFromDB方法
    	    delFromDB(conn);
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DbUtil.close(conn);
       }
		
	}
	private void delFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造插入SQL语句字符串变量sql
			String  sql="delete from t_family where familycode=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.familycode);
		
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
			
		}
	
	}
	
	public List<Family> family()throws Exception{
		List<Family> familyList=new ArrayList<Family>();
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement("select * from t_family where 1=1");
			rs=pstmt.executeQuery();
	
			while(rs.next()){
				Family family = new Family();
				family.setFamilycode(rs.getString("familycode"));
				family.setAreacode(rs.getString("areacode"));
				family.setFamilyattribute(rs.getString("familyattribute"));
				family.setPopulation(rs.getString("population"));
				family.setFarmpopulation(rs.getString("farmpopulation"));
				family.setFamilyaddress(rs.getString("familyaddress"));
				family.setCreattime(rs.getTimestamp("creattime"));
				family.setRegistrar(rs.getString("registrar"));
				family.setFamilyholdername(rs.getString("familyholdername"));
				familyList.add(family);
			
			}
			
		}catch(SQLException e){
			
			throw e;
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	
		return familyList;
	}
}
